using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class OrcAI : MonoBehaviour
{
    private Animator animator;
    private Rigidbody2D rb;
    private DetectionZone detectionZone;
    private bool _isAttacking;

    public float speed = 3;

    public void OnAttackStart() => SetAttacking(true);
    public void OnAttackEnd() => SetAttacking(false);

    void Start()
    {
        animator = GetComponent<Animator>();
        rb = GetComponent<Rigidbody2D>();
        detectionZone = GetComponentInChildren<DetectionZone>();
    }

    void FixedUpdate()
    {
        if (detectionZone.detectedObjs != null && !_isAttacking)
        {
            Vector2 direction = (detectionZone.detectedObjs.transform.position - transform.position).normalized;

            rb.AddForce(direction.normalized * speed);

            animator.SetFloat("Orc_X", direction.x);
            animator.SetFloat("Orc_Y", direction.y);
            animator.SetBool("isMoving", true);
        }
        else
        {
            rb.velocity = Vector2.zero;
            animator.SetBool("isMoving", false);
        }
    }

    public void SetAttacking(bool state) => _isAttacking = state;
}